﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PlainOldVersionControl.Domain;
using PlainOldVersionControl.Data;
using PlainOldVersionControl.Data.Base;
using System.IO;
using System.Data.SQLite;

namespace PlainOldVersionControl.BusinessLogic.Base
{
    public class BlobBL : BaseBL
    {
        #region Constructor

        public BlobBL(Blob b) : base(b) { }

        #endregion

        #region Properties

        internal new Blob DomainObject
        {
            get { return base.DomainObject as Blob; }
        }

        #endregion

        #region Public Methods

        public override void Validate()
        {
            // TODO : put in validation
        }

        #endregion

        #region Public Static Methods

        public static void InitializeNewDataBase(string rootPath)
        {
            CreateNewDataBase(rootPath);
        }

        public static void RetreiveFromObjectStore(string hash, string dstFilePath)
        {
            ObjectStoreDA.RetreiveFile(hash, dstFilePath);
        }

        public static BlobCompare CompareFilesFromObjectStore(string hashA, string hashB)
        {
            BlobCompare retVal = new BlobCompare();
            string dstA = Path.Combine(Settings.TempDirRoot, Guid.NewGuid().ToString());
            string dstB = Path.Combine(Settings.TempDirRoot, Guid.NewGuid().ToString());

            RetreiveFromObjectStore(hashA, dstA);
            RetreiveFromObjectStore(hashB, dstB);

            retVal.FileAPath = dstA;
            retVal.FileBPath = dstB;

            return retVal;
        }
        
        #endregion

        #region Private Static Methods

        private static void CreateNewDataBase(string rootPath)
        {
            if (File.Exists(Settings.CurrentDatabase))
                File.Delete(Settings.CurrentDatabase);


            SQLiteCommand cmd = DA.GetCommand();
            cmd.CommandText = "create table blobs ( hash TEXT, filehash TEXT, treehash TEXT, filename TEXT );";
            cmd.ExecuteNonQuery();

            cmd.CommandText = "create table trees ( hash TEXT, directoryname TEXT );";
            cmd.ExecuteNonQuery();

            cmd.CommandText = "create table tree_trees ( parenttreehash TEXT , treehash TEXT );";
            cmd.ExecuteNonQuery();

            cmd.CommandText = "create table tree_blobs ( treehash TEXT , blobhash TEXT );";
            cmd.ExecuteNonQuery();

            cmd.CommandText = "create table commits ( hash TEXT , author TEXT, message TEXT, createddate DATETIME, treehash TEXT, parentcommithash TEXT );";
            cmd.ExecuteNonQuery();

            cmd.CommandText = "create table settings ( id int , settingvalue TEXT );";
            cmd.ExecuteNonQuery();





            cmd.CommandText = "insert into settings ( id, settingvalue ) values (1, '');";
            cmd.ExecuteNonQuery();

            cmd.CommandText = string.Format("insert into settings ( id, settingvalue ) values (2, '{0}');", rootPath);
            cmd.ExecuteNonQuery();

        }

        #endregion      
    
        public class BlobCompare
        {
            public string FileAPath { get; set; }
            public string FileBPath { get; set; }
        }
    }
}
